package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.converter.EmployeeDTOConverter;
import com.jingdianjichi.subject.application.dto.EmployeeDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.eneity.EmployeeBO;
import com.jingdianjichi.subject.infra.basic.service.IEmployeeService;
import com.jingdianjichi.subject.service.EmployeeDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Resource
    private EmployeeDomainService service;
    @RequestMapping("/addEmployee")
    public Result<Boolean> addE(@RequestBody EmployeeDTO employeeDTO) {
        try {
            // 这个方法的作用是检查当前日志记录级别是否允许记录信息级别的日志消息。如果允许，则返回true；否则返回false。
            // 假设日志记录级别设置为“info”，那么调用log.isInfoEnabled()将返回true，因为信息级别的日志消息是允许的。

            String number = service.getNumber();
            employeeDTO.setId(0);

            employeeDTO.setEmployeeNumber(number);
            if (log.isInfoEnabled()) {
                    log.info("EmployeeController.add.dto:{} ", JSON.toJSONString(employeeDTO));
            }
            System.out.println(employeeDTO);
            Preconditions.checkNotNull(employeeDTO.getName(), "name不能为空");
            Preconditions.checkNotNull(employeeDTO.getDepartmentName(), "部门不能为空");
            Preconditions.checkNotNull(employeeDTO.getTel(), "手机号不能为空");
            Preconditions.checkNotNull(employeeDTO.getUsername(), "username不能为空");
            EmployeeBO employeeBO = EmployeeDTOConverter.INSTANCE.converterDtoToEmployeeBo(employeeDTO);
            service.addEmployee(employeeBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("EmployeeController.addE.error", e.getMessage(), e);


            return Result.fail(e.getMessage());
        }
    }

    @RequestMapping("/delEmployee")
    @ResponseBody
    public Result<Boolean> delEmployee(@RequestBody EmployeeDTO employeeDTO) {
        try {
            // 这个方法的作用是检查当前日志记录级别是否允许记录信息级别的日志消息。如果允许，则返回true；否则返回false。
            // 假设日志记录级别设置为“info”，那么调用log.isInfoEnabled()将返回true，因为信息级别的日志消息是允许的。
            if (log.isInfoEnabled()) {
                log.info("EmployeeController.add.dto:{} ", JSON.toJSONString(employeeDTO));
            }
            EmployeeBO employeeBO = EmployeeDTOConverter.INSTANCE.converterDtoToEmployeeBo(employeeDTO);
            service.delEmployee(employeeBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("EmployeeController.addE.error", e.getMessage(), e);

            return Result.fail(e.getMessage());
        }

    }

    @RequestMapping("/updateEmployee")
    @ResponseBody
    public Result<Boolean> updateEmployee(@RequestBody EmployeeDTO employeeDTO) {
        try {
            // 这个方法的作用是检查当前日志记录级别是否允许记录信息级别的日志消息。如果允许，则返回true；否则返回false。
            // 假设日志记录级别设置为“info”，那么调用log.isInfoEnabled()将返回true，因为信息级别的日志消息是允许的。
            if (log.isInfoEnabled()) {
                log.info("EmployeeController.add.dto:{} ", JSON.toJSONString(employeeDTO));
            }
            EmployeeBO employeeBO = EmployeeDTOConverter.INSTANCE.converterDtoToEmployeeBo(employeeDTO);
            service.updateEmployee(employeeBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("EmployeeController.addE.error", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    // 查询全部
    @RequestMapping("/queryAll")

    public Result<List<EmployeeDTO>> queryAll() {
        try {
            List<EmployeeBO> employeeBOS = service.queryAll();

            List<EmployeeDTO> employeeDTOS = EmployeeDTOConverter.INSTANCE.converterEmployeeBoToDto(employeeBOS);
            if (log.isInfoEnabled()) {
                log.info("EmployeeController.queryAll.dto:{} ", JSON.toJSONString(employeeDTOS));
            }
            return Result.ok(employeeDTOS);
        } catch (Exception e) {
            log.error("EmployeeController.queryAll.error", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }
//    @RequestMapping("/queryAll")
//    @ResponseBody
//    public Result<List<EmployeeDTO>> queryAll() {
//        try {
//            QueryWrapper<Employee> qu = new QueryWrapper<Employee>();
//           // qu.eq("is_deleted",0);
//           // qu.eq("IS_DELETED",0);
//            // 查询全部
//            List<Employee> list = employeeService.list(qu);
//            List<EmployeeDTO> employeeDTOS = EmployeeDTOConverter.INSTANCE.converterEmployeeToDto(list);
//            if (log.isInfoEnabled()) {
//                log.info("EmployeeController.queryAll.dto:{} ", JSON.toJSONString(employeeDTOS));
//            }
//            return Result.ok(employeeDTOS);
//        } catch (Exception e) {
//            log.error("EmployeeController.queryAll.error", e.getMessage(), e);
//            return Result.fail(e.getMessage());
//        }
//    }


    @RequestMapping("/queryAllElement")
    @ResponseBody
    public Result<List<EmployeeDTO>> queryAllElement(@RequestBody EmployeeDTO employeeDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("EmployeeController.queryAllElement.dto:{} ", JSON.toJSONString(employeeDTO));
            }
            // 查询全部
            EmployeeBO employeeBO = EmployeeDTOConverter.INSTANCE.converterDtoToEmployeeBo(employeeDTO);
            List<EmployeeBO> employeeBOS = service.queryAllElement(employeeBO);
            // 我要返回的是EmployeeDTO对象 将employeeBOS转化为employeeDTO
            List<EmployeeDTO> employeeDTOS = EmployeeDTOConverter.INSTANCE.converterEmployeeBoToDto(employeeBOS);
            if (log.isInfoEnabled()) {
                log.info("EmployeeController.queryAll.dto:{} ", JSON.toJSONString(employeeDTOS));
            }
            return Result.ok(employeeDTOS);
        } catch (Exception e) {
            log.error("EmployeeController.queryAll.error", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

@RequestMapping("/deleteByIds")
@ResponseBody
public Result<Boolean> deleteByIds(@RequestBody List<Integer> ids) {
    try {
        service.deleteByIds(ids);
        if (log.isInfoEnabled()){
            log.info("删除成功");
        }
        return Result.ok(true);

    } catch (Exception e) {
        log.error("EmployeeController.deleteByIds.error", e.getMessage(), e);
        return Result.fail(e.getMessage());
    }
}

}
